package com.application;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.application.bean.UserBean;
import com.application.databinding.ActivityHandlerBinding;
import com.application.util.ARouterConstants;
import com.application.util.NetUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

@Route(path = ARouterConstants.ACTIVITY_URL_HANDLER)
public class HandlerActivity extends AppCompatActivity {
    private ActivityHandlerBinding binding;
    private UserBean userBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_handler);
        binding.setUserBean(userBean);
        binding.setHandler(this);
        binding.setLifecycleOwner(this);
        binding.setResultText(getString(R.string.wait_data_loading));
        binding.tvResult.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    private Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                String strData = (String) msg.obj;
                binding.setResultText(strData);
                parseJsonDataShow(strData);
                Toast.makeText(HandlerActivity.this, "主线程收到来自网络的消息！", Toast.LENGTH_SHORT).show();
            }
        }
    };

    public void parseJsonDataShow(String jsonStr) {
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            String userId = jsonObject.optString("userId");
            String id = jsonObject.optString("id");
            String title = jsonObject.optString("title");
            String body = jsonObject.optString("body");

            binding.tvHandlerUserId.setText(userId);
            binding.tvHandlerId.setText(id);
            binding.tvHandlerTitle.setText(title);
            binding.tvHandlerBody.setText(body);

            Gson gson = new Gson();
            UserBean userBean = gson.fromJson(jsonStr, UserBean.class);

            Log.d("fan", "parseJsonDataShow: " + userBean.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void startHandler(View view) {

        new Thread(() -> {
            String stringFromNet = getStringFromNet();
            Message message = new Message();
            message.what = 0;
            message.obj = stringFromNet;

            mHandler.sendMessage(message);

        }).start();
        Toast.makeText(this, "开启子线程请求网络", Toast.LENGTH_SHORT).show();
    }

    private String getStringFromNet() {

        return NetUtil.doGet();

//        String result="";
//        StringBuilder stringBuilder=new StringBuilder();
//
//        for (int i = 0; i < 100; i++) {
//            stringBuilder.append("字符串"+i).append("\n");
//        }
//
//        try {
//            Thread.sleep(6000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        result=stringBuilder.toString();
//        return result;
    }
}